package com.qing.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.eduservice.entity.pojo.EduCourse;
import com.qing.eduservice.entity.pojo.EduTeacher;
import com.qing.eduservice.service.EduCourseService;
import com.qing.eduservice.service.EduTeacherService;
import com.qing.servicebase.exception.JsonException;
import com.qing.servicebase.utils.ResponseResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName Yjx
 * @Description TODO
 * @date 2022/9/14 16:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/eduservice/index")
@CrossOrigin
public class IndexController {

    private final EduCourseService courseService;

    private final EduTeacherService teacherService;

    public IndexController(EduCourseService courseService, EduTeacherService teacherService) {
        this.courseService = courseService;
        this.teacherService = teacherService;
    }

    //查询前8条热门课程，查询前4条名师
    @GetMapping("/index")
    public ResponseResult index() {
//查询前8条热门课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> eduList = courseService.list(wrapper);
//查询前4条名师
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        List<EduTeacher> teacherList = teacherService.list(wrapperTeacher);
        Map<String, Object> map = new HashMap<>();
        map.put("eduList",eduList);
        map.put("teacherList",teacherList);
        return teacherList!=null?ResponseResult.ofSuccess(map):
                ResponseResult.ofException(new JsonException(201,"操作失败"));
    }
}
